package com.jeff.android.lab.module.widget;

import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.widget.NestedScrollView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * @author: duff
 * @date: 2019/1/11
 * @since: 1.0.0
 */
public class BottomSheetBehaviorFragment extends BaseFragment {

    @BindView(R.id.tv_state)
    TextView mTvState;

    @BindView(R.id.tv_slide)
    TextView mTvSlide;

    @BindView(R.id.nsv)
    NestedScrollView mNestedScrollView;

    BottomSheetBehavior mBehavior = null;

    public final static SparseArray<String> STATES = new SparseArray<>();

    static {
        STATES.append(BottomSheetBehavior.STATE_DRAGGING, "STATE_DRAGGING");
        STATES.append(BottomSheetBehavior.STATE_SETTLING, "STATE_SETTLING");
        STATES.append(BottomSheetBehavior.STATE_EXPANDED, "STATE_EXPANDED");
        STATES.append(BottomSheetBehavior.STATE_COLLAPSED, "STATE_COLLAPSED");
        STATES.append(BottomSheetBehavior.STATE_HIDDEN, "STATE_HIDDEN");
        STATES.append(BottomSheetBehavior.STATE_HALF_EXPANDED, "STATE_HALF_EXPANDED");
    }

    @Override
    protected int getRootLayout() {
        return R.layout.widget_fragment_bottom_sheet_behavior;
    }

    @Override
    protected void initView() {
        mBehavior = BottomSheetBehavior.from(mNestedScrollView);
        mBehavior.setState(BottomSheetBehavior.STATE_SETTLING);
        mBehavior.setBottomSheetCallback(callback);
    }

    BottomSheetBehavior.BottomSheetCallback callback = new
            BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(View view, int i) {
                    Log.d("jeff", "onStateChanged:" + i);

                    mTvState.setText("current state:" + STATES.get(i));
                }

                @Override
                public void onSlide(View view, float v) {
                    Log.d("jeff", "onSlide:" + v);

                    mTvSlide.setText("slide:" + v);
                }
            };

    @OnClick(R.id.btn_expand)
    public void expand() {
        if (mBehavior != null)
            mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

    @OnClick(R.id.btn_collapse)
    public void collapse() {
        if (mBehavior != null)
            mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
    }

    @OnClick(R.id.btn_hide)
    public void hide() {
        if (mBehavior != null) {
            if (mBehavior.isHideable()) {
                mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            } else {
                Toast.makeText(getContext(), "设置不可隐藏", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @OnClick(R.id.btn_half_expand)
    public void halfExpand() {
        if (mBehavior != null)
            mBehavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
    }

}
